#include <dspSimulator/instruction/ldnw.h>

bool LDNW::checkCondition(std::shared_ptr<DspMachine> dspMachine) {
    if (isCompactInstruction) {
        return true;
    } else {
        return checkStandardCondition(dspMachine);
    }
}

void LDNW::loadInstIRIntoJit(llvm::LLVMContext *llvmContext, llvm::Module *module,
                             std::shared_ptr<llvm::IRBuilder<>> irBuilder) {

}

std::shared_ptr<Instruction> LDNW::decode(std::shared_ptr<InstInfo> instInfo, u64 baseCycle) {
    std::shared_ptr<LDNW> ldnw;
    auto dspInstInfo = std::dynamic_pointer_cast<DspInstInfo>(instInfo);
    bool isStandardInst = dspInstInfo->isStandardInstruction();
    if (isStandardInst) {
        auto data = vectorToBitSet<32>(dspInstInfo->getBits());
        // LDW (.unit) *+baseR[offsetR], dst
        if (checkBits<5>(data, 2, 0b01101) && checkBits<1>(data, 8, 0b1) && existStandardCondition(data)) {
            ldnw = std::make_shared<LDNW>(dspInstInfo->getAddress(), baseCycle);
            ldnw->creg_z = extractBits<4>(data, 28);
            ldnw->dst = extractBits<5>(data, 23).to_ulong();
            ldnw->baseR = extractBits<5>(data, 18).to_ulong();
            ldnw->offsetR = extractBits<5>(data, 13).to_ulong();
            ldnw->cst = ldnw->offsetR;
            ldnw->mode = extractBits<4>(data, 9).to_ulong();
            ldnw->y = data[7];
            if (ldnw->y == 0) {
                ldnw->funcUnit = FuncUnit::D1;
            } else {
                ldnw->funcUnit = FuncUnit::D2;
            }
            ldnw->s = data[1];
            ldnw->p = data[0];
            ldnw->instFormat = InstFormat::Kind1;
        }
    }
    return ldnw;
}

void LDNW::executeCustom(std::shared_ptr<TargetMachine> targetMachine, ExecutePhase executePhase) {
    auto dspMachine = std::dynamic_pointer_cast<DspMachine>(targetMachine);
    if (executePhase == ExecutePhase::E1) {
        if (instFormat == InstFormat::Kind1) {
            u32 *baseRReadPtr;
            u32 *baseRWritePtr;
            if (y == 0) {
                baseRReadPtr = dspMachine->getSaveRegs()->getReg32ByName(RegAName[baseR])->getDataPtr();
                baseRWritePtr = dspMachine->getRegisters()->getReg32ByName(RegAName[baseR])->getDataPtr();
            } else {
                baseRReadPtr = dspMachine->getSaveRegs()->getReg32ByName(RegBName[baseR])->getDataPtr();
                baseRWritePtr = dspMachine->getRegisters()->getReg32ByName(RegBName[baseR])->getDataPtr();
            }
            m_visitMemAddr = computeRealAddress(baseRReadPtr, baseRWritePtr, mode, 2, dspMachine);
        }
    } else if (executePhase == ExecutePhase::E3) {
        m_data = dspMachine->getMMU()->getMemory()->readNBytes(m_visitMemAddr, 4);
    } else if (executePhase == ExecutePhase::E5) {
        *dstLoRegPtr = vectorToBitSet<32>(m_data).to_ulong();
    }
}

std::string LDNW::toString() const {
    std::string res;
    if (!isCompactInstruction) {
        res += getStandardConditionString() + " ";
    }
    res += m_name + " " + FuncUnitName[static_cast<u32>(funcUnit)] + " ";
    switch (instFormat) {
        case InstFormat::Kind1:
            res += addressModeToString() + "," + getDstReg32Name();
            break;
        default:
            break;
    }
    return res;
}

std::shared_ptr<DspInstruction> LDNW::clone() {
    return std::make_shared<LDNW>(*this);
}
